package com.gl.saasservice.repository;

import com.gl.saasservice.entity.DepartmentHierarchy;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface DepartmentHierarchyRepository extends JpaRepository<DepartmentHierarchy, Long>, JpaSpecificationExecutor<DepartmentHierarchy> {

    List<DepartmentHierarchy> findByTagIdAndAreaId(Long tagId, Long areaId);

    List<DepartmentHierarchy> findByIdIn(List<Long> ids);

    boolean existsByParentId(Long id);

    @Query(nativeQuery = true, value = "SELECT MAX(id) FROM `saas_department_hierarchy` WHERE parent_id = :parentId")
    Long findByParentIdMaxId(@Param("parentId") Long parentId);

    List<DepartmentHierarchy> findByOrgIdIn(List<Long> orgIds);

    List<DepartmentHierarchy> findByAreaIdInAndStatus(List<Long> areaId, Integer status);

    List<DepartmentHierarchy> findByAreaIdAndName(Long areaId, String name);

    List<DepartmentHierarchy> findByName(String name);

    DepartmentHierarchy findByOrgIdAndStatus(Long orgId, Integer status);
    DepartmentHierarchy findByOrgId(Long orgId);

    DepartmentHierarchy findByParentId(Long parentId);

    //@Query("SELECT social_credit_number FROM saas_department_hierarchy WHERE social_credit_number is not :socialCreditNumber ")
    @Query(nativeQuery = true, value = "SELECT * FROM saas_department_hierarchy WHERE social_credit_number is not :socialCreditNumber")
    List<DepartmentHierarchy> findSocialCreditNumberIsNotNull(@Param("socialCreditNumber") String socialCreditNumber);

}
